﻿using System;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Volo.Abp.ObjectMapping;
using Microsoft.AspNetCore.Authorization;
using LF.Domain.Services;
using LF.Domain.Entities;
using LF.Shared.Dtos.Input;
using LF.Shared.Dtos.Out;
using AutoMapper;
using Volo.Abp.Uow;
using LF.Common;
using Volo.Abp.DependencyInjection;
using LF.Application.Helpers;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Utilities;

namespace LF.Application.Services.Iml
{
    [Authorize]
    public class TestService : BaseService, ITestService
    {
        IAppManagerService _service;
        public TestService(IObjectMapper<LFModule> mapper, IAppManagerService service,
            IGlobalManagerService globalManager) : base(mapper, globalManager)
        {
            _service = service;
        }

        public string JwtValidate()
        {
            return "Jwt OK!";
        }
       
        
        [AllowAnonymous]
        public List<AppDicOut> GetDic(AppDicSearch search)
        {
            if (!LFConfig.Debug)
            {
                return new List<AppDicOut>();
            }

            var list = new List<AppDic>();
            if (string.IsNullOrEmpty(search.Key))
            {
                list = _service.GetAppDic(search.Index, search.PageSize);
            }
            else
            {
                list = _service.GetAppDic(search.Index, search.PageSize, search.Key);
            }
            var result = _mapper.Map<List<AppDic>, List<AppDicOut>>(list);
            return result;
        }
        [UnitOfWork]
        public virtual async Task<Result> DataAsync()
        {
            if (!LFConfig.Debug)
            {
                return Error("非法调用");
            }

            _service.AddLog(new AppLog { Id = Guid.NewGuid(), Module = nameof(TestService), Value = "test", CreatTime = DateTime.Now });
            var i = 0;
            var j = 1;
            var x = j / i;
            await Task.CompletedTask;
            return Ok();
        }
        [AllowAnonymous]
        public string Test()
        {
            return "xx";
        }
        /// <summary>
        /// 测试打印码
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public async Task<FileContentResult?> GetQrCodeAsync(string number)
        {
            if (!LFConfig.Debug)
            {
                return null;
            }
            byte[] bytes = await FileHelper.GetQrImage(number);
            var file = new FileContentResult(bytes ?? Array.Empty<byte>(), "application/x-png")
            {
                FileDownloadName = $"{number}.png"
            };
            return file;

        }
    }
}

